import { router } from '../router';
import { API } from '@v/utils/API';
import { defineStore } from 'pinia';
import { addSCST } from '@v/utils/SCSA';
import { ColorScheme, Modes } from '@p/utils';
import { ConfigFile } from '@p/main-config-file';
import { MdFileInfo, ImageInfo, MessageObject2 } from '@p/main-windowObject';
import { MessageObject, MessageAcceptType, MessageType } from '@type/WindowService';

interface StateData {
	/**
	 * 窗口id
	 */
	id: number;
	/**
	 * 项目名称
	 */
	name: string;
	/**
	 * 项目版本
	 */
	version: string;
	/**
	 * Markdown 文件信息
	 */
	fileInfo?: MdFileInfo;
	/**
	 * Markdown 文件信息消息 id
	 */
	fileInfoMessageId: number;
	/**
	 * 配色方案
	 */
	colorScheme: ColorScheme;
	/**
	 * 当前操作文件路径
	 */
	filePath?: string;
	/**
	 * 是否进入工作页面
	 */
	isWork: boolean;
	/**
	 * 抽屉是否展示
	 */
	drawer_isShow: boolean;
	/**
	 * 抽屉展示组件
	 */
	drawer_showComponent?: Modes;
	/**
	 * 消息
	 */
	message: {
		/**
		 * 最新的消息
		 */
		latest?: MessageObject;
		/**
		 * 消息列表
		 */
		list: MessageObject[];
		/**
		 * 已查看消息最后一条的id
		 */
		seen: number;
		/**
		 * 是否展示消消息中心
		 */
		show: boolean;
	};
	/**
	 * 工作数据
	 */
	work: {
		/**
		 * 是否处于工作中
		 */
		ongoing: boolean;
		/**
		 * 当前模式
		 */
		currentMode: Modes | null;
	};
	/**
	 * 切换颜色模式过度时间
	 */
	SCST: number;
	/**
	 * 配置文件
	 */
	configFile?: ConfigFile;
}

export const useRootStore = defineStore('root', {
	state(): StateData {
		return {
			id: -1,
			name: '',
			version: '',
			fileInfo: undefined,
			fileInfoMessageId: -1,
			colorScheme: 'dark',
			filePath: undefined,
			isWork: false,
			drawer_isShow: false,
			drawer_showComponent: undefined,
			message: {
				latest: undefined,
				list: [],
				seen: 0,
				show: false,
			},
			work: {
				ongoing: false,
				currentMode: null,
			},
			SCST: 300,
			configFile: undefined,
		};
	},
	actions: {
		setId(id: number) {
			this.id = id;
		},
		setName(n: string) {
			this.name = n;
		},
		setVersion(v: string) {
			this.version = v;
		},
		setFilePaht(filePath: string | undefined) {
			this.filePath = filePath;
		},
		setFileInfo(obj: MdFileInfo | undefined) {
			if (obj) {
				this.fileInfo = obj;

				this.setIsWork(true);

				document.title = obj.fileName;
			} else {
				document.title = this.name;
			}
		},
		setFileInfoMessageId(id: number) {
			this.fileInfoMessageId = id;
		},
		setIsWork(s: boolean) {
			if (this.isWork !== s) {
				if (s) {
					router.push('/work');
				} else {
					router.push('/home');
				}

				this.isWork = s;
			}
		},
		setMessage(type: MessageAcceptType, obj: MessageObject2) {
			const find = (obj: MessageObject2) => {
				let index = -1;

				// 反向查找
				for (let i = this.message.list.length - 1; i >= 0; i--) {
					if (obj.id === this.message.list[i].id) {
						index = i;
						break;
					}
				}

				if (index !== -1) {
					this.message.list[index] = obj;
					this.message.latest = obj;
				}
			};

			switch (type) {
				case 'insert':
					this.message.list.push(obj);
					this.message.latest = obj;
					break;
				case 'update':
					find(obj);
					break;
			}
		},
		async addMessage(type1: MessageAcceptType, type2: MessageType, message: string) {
			const id = await API.invoke('message-id-add');

			this.setMessage(type1, { id, type: type2, message });

			return id;
		},
		removeMessage(id: number) {
			const index = this.message.list.findIndex((t) => t.id === id);

			this.message.list.splice(index, 1);
		},
		setMessageSeen(s: number) {
			this.message.seen = s;
		},
		setNotificationsCenterShow(s: boolean) {
			this.message.show = s;
		},
		setDrawer_isShow(s: boolean) {
			this.drawer_isShow = s;
		},
		setImgInfo_base64URL(id: number, base64URL: string) {
			if (this.fileInfo) {
				for (let i = 0; i < this.fileInfo.imageInfo.length; i++) {
					if (this.fileInfo.imageInfo[i].id === id * 1) {
						this.fileInfo.imageInfo[i].base64URL = base64URL;
						break;
					}
				}
			}
		},
		setImgInfo_loadResult(id: number, s: boolean, type?: ImageInfo['loadType']) {
			if (this.fileInfo) {
				for (let i = 0; i < this.fileInfo.imageInfo.length; i++) {
					if (this.fileInfo.imageInfo[i].id === id * 1) {
						this.fileInfo.imageInfo[i].loadResult = s;
						if (type) this.fileInfo.imageInfo[i].loadType = type;
						break;
					}
				}
			}
		},
		setDrawer_showComponent(name: Modes | undefined) {
			this.drawer_showComponent = name;
		},
		setColorScheme(s: ColorScheme, y?: boolean) {
			this.colorScheme = s;

			addSCST(s, this.SCST);

			if (!y) {
				API.send('set-color-scheme', s);
			}
		},
		setWork(s1: boolean, s2: Modes | null) {
			this.work.ongoing = s1;
			this.work.currentMode = s2;
		},
		setConfigFile(value: ConfigFile) {
			this.configFile = value;
		},
	},
	getters: {
		/**
		 * 未查看消息的数量
		 */
		notSeenMessageCounts(store) {
			const obj = {
				total: 0,
				info: 0,
				error: 0,
				warning: 0,
				success: 0,
			};

			for (let i = store.message.list.length - 1; i >= 0; i--) {
				if (store.message.list[i].id === this.message.seen) {
					break;
				} else {
					obj.total++;

					switch (store.message.list[i].type) {
						case 'success':
							obj.success++;
							break;
						case 'warning':
							obj.warning++;
							break;
						case 'error':
							obj.error++;
							break;
						case 'info':
							obj.info++;
							break;
					}
				}
			}

			return obj;
		},
	},
});
